package dxcorp.dxadminapi.utils;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec;

public class TokenUtil {

    public static Object getTokenUserId() {
        String token = getRequest().getHeader("token");// 从 http 请求头中取出 token

        String userId = JWT.decode(token).getAudience().get(0);
//
//        String username = JWT.decode(token).getClaim("username").asString();
//        String userId = JWT.decode(token).getClaim("userId").asString();
//
//        JSONObject jsonObject = new JSONObject();
        JSONObject info = new JSONObject();
//
        info.put("roles","admin");
        info.put("introduction", "I am a super administrator");
        info.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        info.put("name", "Super Admin");
//
//        jsonObject.put("code", 20000);
//        jsonObject.put("data", info);

        return info;
    }

    public static Object getTokenLogout() {
        String token = getRequest().getHeader("token");// 从 http 请求头中取出 token
//        System.out.println(JWT.decode(token).getAudience());
//        String userId = JWT.decode(token).getAudience().get(0);

        String username = JWT.decode(token).getClaim("username").asString();
        String userId = JWT.decode(token).getClaim("userId").asString();

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("code", 20000);
        jsonObject.put("data", "success");

        return jsonObject;
    }

    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }
}
